package com.ttl.bc.ic.stktransfer2;

import hibernate.util.HibernateUtil;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ttl.common.db.ADAO;

public class BCStkTransfer2DAO extends ADAO {

	@SuppressWarnings("unchecked")
	public boolean update(String s, Object o) {
		Session session = null;

		BCStkTransfer2 st = (BCStkTransfer2)o;
		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCStkTransfer2 where docNo = '"+s+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	BCStkTransfer2 h = (BCStkTransfer2)resultList.get(i);
            	
            	st.setCreatorCode(h.getCreatorCode());
            	st.setCreateDateTime(h.getCreateDateTime());
                session.delete(h);
            }            
            
			session.save(o);
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();
			return false;
		}finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public boolean cancel(String docNo) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCStkTransfer2 where docNo = '"+docNo+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	BCStkTransfer2 h = (BCStkTransfer2)resultList.get(i);
            	h.setIsCancel(1);
            	Iterator it = h.getBcStkTransfSubs().iterator();
            	while (it.hasNext()) {
					BCStkTransfSub2 sub = (BCStkTransfSub2) it.next();
					sub.setIsCancel(1);
				}
                session.save(h);
            }            
            
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();
			return false;
		}finally {
			session.close();
		}	
	}
}
